package com.burpee.web;



import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.burpee.ui.Member;



public class LoginInterceptor extends HandlerInterceptorAdapter {

	Map<String, String> loginActionParams = new HashMap<String, String>();
	public LoginInterceptor() {
		loginActionParams.put("myFood", "login");
		loginActionParams.put("mySports", "login");

	}
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String action = request.getParameter("action");

		if (loginActionParams.containsKey(action)) {

			Member member = (Member)request.getSession().getAttribute("login");

			if (member == null) {

				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();

				String script = "<script>alert('로그인을 먼저 해주세요...');</script>";

				out.print(script);

				return false;
			}
		}
		
		return super.preHandle(request, response, handler);
	}
}
